package top.novel.setting;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SearchableConfigurable;
import com.intellij.openapi.util.NlsContexts;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.novel.persistence.PersistenceData;
import top.novel.persistence.PersistenceService;
import top.novel.ui.SettingUI;

import javax.swing.*;

/**
 * @author xuhainan
 * @date 2022/8/8 17:09
 * @region hefei
 */
public class NovelPluginSetting implements SearchableConfigurable {

    private SettingUI settingUI;

    @Override
    public @NotNull @NonNls String getId() {
        return "novel.plugin.setting";
    }

    @Override
    public @NlsContexts.ConfigurableName String getDisplayName() {
        return "Novel.Setting";
    }

    @Override
    public @Nullable JComponent createComponent() {
        if (settingUI == null) {
            settingUI = new SettingUI();
        }
        return settingUI.getComponent();
    }

    @Override
    public boolean isModified() {
        return true;
    }

    @Override
    public void apply() throws ConfigurationException {
        String url = settingUI.getUrlTextField().getText();
        if (StrUtil.isNotEmpty(url)) {
            PersistenceService persistenceService = PersistenceService.getInstance();
            PersistenceData persistenceData = persistenceService.getPersistenceData();
            persistenceData.setUrl(url);
            persistenceData.setNextUrl(null);
            persistenceData.setPreUrl(null);
            String urlState = persistenceData.getUrl();
            System.out.println("urlState value next >> " + urlState);
        }
    }
}
